package core

import (
	"alertmanagerWebhook/global"
	"fmt"

	"github.com/gomodule/redigo/redis"
)

func ConnRedis() (redis.Conn, error) {
	c, err := redis.Dial("tcp", global.Config.Redis.RedisAddr())
	if err != nil {
		return nil, fmt.Errorf("connect redis failed: %v\n", err)
	}

	if global.Config.Redis.RedisPassword != "" {
		if _, err = c.Do("AUTH", global.Config.Redis.RedisPassword); err != nil {
			c.Close()
			return nil, fmt.Errorf("redis password failed: %v\n", err)
		}
	}

	return c, nil
}
